package com.deneme;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import functions.images.AlgorithmsForCloudProject;

public class Facebook extends HttpServlet {
    private static BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

	public void doGet(HttpServletRequest req,
			HttpServletResponse resp)
					throws IOException {
		/* get the url for the picture from facebook */
		String urlStr = req.getParameter("img-url");
		/* create a URL from the string representation */
        URL url = new URL(urlStr);
        /* open a strem to read from the url */
        ByteArrayOutputStream bais = new ByteArrayOutputStream();
		InputStream is = null;
		is = url.openStream();
		byte[] byteChunk = new byte[4096]; // Or whatever size you want to read
											// in at a time.
		int n;

		/* read in file data a chunk at a time */
		while ((n = is.read(byteChunk)) > 0) {
			bais.write(byteChunk, 0, n);
		}

		byteChunk = bais.toByteArray();
		/* create an image from the byte data */
		Image img = ImagesServiceFactory.makeImage(byteChunk);
		/* check to make sure the image is of reasonable dimensions */
		if (img.getHeight() > 800|| img.getWidth() > 800) {
			/* if the image is too large, redirect to custom error page */
			req.setAttribute("errormsg", "Image is too large.  Please only use images less than 800 x 800 pixels.");
			RequestDispatcher rd = req.getRequestDispatcher("/error");
			try {
				rd.forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			}
		}
		/* upload the image to the blobstore */
		BlobKey blobkey;
		try {
			blobkey = AlgorithmsForCloudProject.please(img);
			/* add the blob key to the user's saved images data */
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			UserData data = UserData.getDataForUser(user);
			data.addImageKey(blobkey);
			/* forward the user to the puzzle config page to create a new puzzle with this image */
	    	RequestDispatcher rd = req.getRequestDispatcher("/puzzleconfiguration?blob-key=" + blobkey.getKeyString());
	    	rd.forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}		
}
